# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import logging

from odoo import api, fields, models, _
from odoo.exceptions import AccessDenied, UserError

_logger = logging.getLogger(__name__)


ORDER_TYPE= [
        ('passenger', '客调令'),
        ('freight', '货调令')]

class CrDispatchingOrder(models.Model):
    _name = "cr.dispatching.order"
    _description = "CrDispatchingOrder"

    name = fields.Char()
    order_type = fields.Selection(ORDER_TYPE, default='passenger')

    date = fields.Date()
    act_time = fields.Datetime()
    note = fields.Text()
    line_ids = fields.One2many('cr.dispatching.order.line', 'order_id')

    @api.model
    def find_or_create(self, values):
        name = values['name']
        order_type = values['order_type']
        domain = [['name','=',name],['order_type','=',order_type]]
        rec = self.search(domain, limit=1)
        if rec:
            return rec

        return self.create(values)


class CrDispatchingOrderLine(models.Model):
    _name = "cr.dispatching.order.line"
    _description = "CrDispatchingOrderLine"

    order_id = fields.Many2one('cr.dispatching.order')
    order_no = fields.Char(related='order_id.name', store=True)
    order_type = fields.Selection(ORDER_TYPE, related='order_id.order_type', store=True)
    sequence = fields.Integer()
    group_sequence = fields.Integer()
    start_date = fields.Date()
    end_date = fields.Date()
    train_no = fields.Char()
    content = fields.Text()

    @api.model
    def create_for_passenger(self, values):
        values['order_type'] = 'passenger'
        return self.create_with_order(values)

    @api.model
    def create_for_freight(self, values):
        values['order_type'] = 'freight'
        return self.create_with_order(values)

    @api.model
    def create_with_order(self, values):
        order_no = values['order_no']
        order_type = values['order_type']
        vals = values.copy()
        del vals['order_no']
        del vals['order_type']
        order = self.order_id.find_or_create({
            'name': order_no, 'order_type':order_type })
        vals['order_id'] = order.id
        order_line = self.create(vals)

        order_line.order_id.act_time = min(order_line.order_id.line_ids.mapped('start_date'))
        return order_line


    def write_for_passenger(self, values):
        values['order_type'] = 'passenger'
        return self.write_with_order(values)

    def write_for_freight(self, values):
        values['order_type'] = 'freight'
        return self.write_with_order(values)

    def write_with_order(self, values):
        order_no = values['order_no']
        order_type = values['order_type']
        vals = values.copy()
        del vals['order_no']
        del vals['order_type']
        order = self.order_id.find_or_create({
            'name': order_no, 'order_type':order_type })
        vals['order_id'] = order.id
        ret = self.write(vals)

        self.order_id.act_time = min(self.order_id.line_ids.mapped('start_date'))

        return ret





